﻿app.controller('RelationshipController', ['$scope', 'GlobalService', 'fileUpload', 'ApplicationService', 'alertboxservice', function ($scope, GlobalService, fileUpload, ApplicationService, alertboxservice) {

    $scope.currentPage = 1;
    $scope.searchkey = "";
    $scope.sortmode = "ASC";
    $scope.sortby = "RELATIONSHIPNAME";
    $scope.showgridloader = true;

    BindGrid();

    $scope.sort = function () {
        if ($scope.sortmode == 'ASC') {
            $scope.sortmode = 'DESC';
        }
        else {
            $scope.sortmode = 'ASC';
        }
        BindGrid();
    }

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Relationship";
                $scope.Relation = null;
                break;
            case 'edit':
                $scope.title = "Edit Relationship";
                url = "GetRelationShip";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.Relation = data;
                });
                break;
        }
        $("#Relationship").modal({ backdrop: 'static' });
    }

    function BindGrid() {
        $scope.showgridloader = true;
        url = "GetRelationShips?PageNo=" + $scope.currentPage + "&SearchKey=" + $scope.searchkey + "&Sort=" + $scope.sortby + "&SortMode=" + $scope.sortmode;
        ApplicationService.Getdata(url).success(function (data) {
            $scope.showgridloader = false;
            $scope.list = data;
            if (data.length > 0) {
                $scope.entryLimit = 10;
                $scope.filteredItems = data[0].TotalCounts;
                $scope.totalItems = data[0].TotalCounts;
            }
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
        BindGrid();
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "DeleteRelationShip";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
    }

    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "AddRelationShip";
                break;
            case 'edit':
                url = "UpdateRelationShip";
                break;
        }
        ApplicationService.Save($scope.Relation, url).success(function (result) {
            alertboxservice.ShowMsg(result, "Relationship ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }

    $scope.Search = function () {
        BindGrid();
    }

    $scope.Relationshiptype = [{ RelationshipId: 1, RelationshipType: "Married" }, { RelationshipId: 2, RelationshipType: "Un Married" }]

}]);

